//
//  JMTabBarController.m
//  OpenSesame
//
//  Created by zhangjiaming on 15/12/21.
//  Copyright © 2015年 OpenSesame. All rights reserved.
//

#import "JMTabBarController.h"
#import "JMTabBar.h"
#import "JMNavigationController.h"
#import "JMHomeViewController.h"
#import "JMProfileViewController.h"
#import "JMFrequencyViewController.h"
#import "JMCustomView.h"
#import "MBProgressHUD+JM.h"
#import "JMInstructionController.h"

@interface JMTabBarController () <JMTabBarDelegate, JMHomeViewControllerDelegate>
@property (weak, nonatomic) JMTabBar *customTabBar;

/** home需要的透明蒙版 */
@property (strong, nonatomic) UIView *homeShieldView;

@property (strong, nonatomic) NSMutableArray *array;

@end

@implementation JMTabBarController
- (UIView *)homeShieldView {
    if (!_homeShieldView) {
        _homeShieldView = [[UIView alloc] init];
        _homeShieldView.frame = CGRectMake(0, 0, JMPhoneWidth, JMPhoneHeight);
        _homeShieldView.backgroundColor = [UIColor clearColor];
    }
    return _homeShieldView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 初始化tabbar
    [self setUpTabBar];
    
    // 初始化子控制器
    [self setUpChildControllers];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    // 移除之前的UITabBarButton
    for (UIView *child in self.tabBar.subviews) {
        if ([child isKindOfClass:[UIControl class]]) { //UITabBarButton
            [child removeFromSuperview];
        }
    }
}

/**
 *  初始化子控制器
 */
- (void)setUpChildControllers {
    // 频率检测
    JMFrequencyViewController *frequencyVC = [[JMFrequencyViewController alloc] init];
    [self setUpChildController:frequencyVC imageName:@"检测" selectedImageName:@"检测 选中"];
    
    // 首页
    JMHomeViewController *homeVC = [[JMHomeViewController alloc] init];
    homeVC.delegate = self;
    self.delegates = (id)homeVC;
    [self setUpChildController:homeVC imageName:@"首页 未选" selectedImageName:@"首页"];
    
    // 我的
    JMProfileViewController *profileVC = [[JMProfileViewController alloc] init];
    [self setUpChildController:profileVC imageName:@"我的" selectedImageName:@"我的 选中"];
    
    NSMutableArray *arr = [NSMutableArray array];
    [arr addObject:frequencyVC];
    [arr addObject:profileVC];
    self.array = arr;
}

/**
 *  添加一个子控制器
 *
 *  @param vc                子控制器
 *  @param imageName         图片名
 *  @param selectedImageName 选中时的图片名
 */
- (void)setUpChildController:(UIViewController *)vc imageName:(NSString *)imageName selectedImageName:(NSString *)selectedImageName {
    // 设置首页导航栏
    vc.tabBarItem.image = [UIImage imageNamed:imageName];
    vc.tabBarItem.selectedImage = [UIImage imageNamed:selectedImageName];
    vc.title = @"X-007";
    
    // 包装一个导航控制器
    JMNavigationController *navigationVC = [[JMNavigationController alloc] initWithRootViewController:vc];
    [self addChildViewController:navigationVC];
    navigationVC.view.backgroundColor = JMColor(242, 242, 242);
    
    // 添加按钮
    [self.customTabBar addTabBarButtonWithItem:vc.tabBarItem];
}

/**
 *  初始化tabBar
 */
- (void)setUpTabBar {
    JMTabBar *customTabBar = [[JMTabBar alloc] init];
    customTabBar.delegate = self;
    customTabBar.frame = self.tabBar.bounds;
    [self.tabBar addSubview:customTabBar];
    self.customTabBar = customTabBar;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.homeShieldView removeFromSuperview];
    if ([self.delegates respondsToSelector:@selector(tabBarController:didDismissHomeShieldView:)]) {
        [self.delegates tabBarController:self didDismissHomeShieldView:self.homeShieldView];
    }
    
}

#pragma mark - JMTabBarDelegate方法
- (void)tabBar:(JMTabBar *)tabBar didSelectButtonFrom:(long)from to:(long)to {
    self.selectedIndex = to;
}

#pragma mark - JMHomeViewControllerDelegate
- (void)homeViewController:(JMHomeViewController *)homeVC willDisPlayHideView:(UIView *)hideView {
    // 添加home需要的蒙版
    [self.view addSubview:self.homeShieldView];
    
    // 将home显示的隐藏View插入到homeShieldView的上面
    [self.view insertSubview:hideView aboveSubview:self.homeShieldView];
}

- (void)homeViewController:(JMHomeViewController *)homeVC willDismissHideView:(UIView *)hideView {
    [self.homeShieldView removeFromSuperview];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
